* density_graphs_binary.do	JEP	02/27/2015
* Plots the evolution of wage densities across months, for treated and control group
* Monthly wage, binary treatment

*************************** Change log ********************************** 

/*
	04/20/15	JEP		Add histograms
*/	

clear 
version 13.1
/*
* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" loc master "B:\Col_Minimum_Wage"
	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
	loc pr=0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'" 
	* Project calls
	project, uses("`master'/Data/Source/ocupados_mw.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/build_mw_measures.do")
	project, relies_on("`master'/Code/Est/build_treatment.do")
	project, relies_on("`master'/Code/Est/export_rif.do")
	project, relies_on("`master'/Code/Est/confirmedrun.do")
}
*/
loc pr = 0
loc master "\\bmstginveco\Salario_Minimo\Col_Minimum_Wage"

* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/build_mw_measures.do"
do "`master'/Code/Est/build_treatment.do"
* do "`master'/Code/Est/export_rif.do"
* do "`master'/Code/Est/confirmedrun.do"

* Define a program to export graphs
//*-----------------------------Formal-------------------------------------//
	cap program drop mex
	program define mex
		cap mkdir "`2'/Doc"
		cap mkdir "`2'/Doc/tosubmit2"
		cap mkdir "`2'/Doc/tosubmit2/fig"
		graph export "`2'/Doc/tosubmit2/fig/`1'.pdf", replace
		graph export "`2'/Doc/tosubmit2/fig/`1'.png", replace
		graph export "`2'/Doc/tosubmit2/ILR-Figs/FigB3_`1'.png", replace
	end	
foreach yyyy in  1997  {
	foreach tr in 1 {
		loc tt = "`yyyy'q`tr'"
		use "`master'/Data/Source/ocupados_mw.dta", clear
		* Choose sample
		mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal 
		* Build mw measures
		loc date = tq(`tt')
		loc date = `date' - 1
		mw_measures rif4sample , stub(rif4) time("`date'")
		* Build treatment
		mytreatment, posts("tq(`tt')") mw(bite_rif4_ind) wage(salario_mensual_real)

		sum minw_mensual_real if time==tq(`tt'), meanonly
		glo line=ln(r(mean))
		glo bw=0.08

		* Build graphs without separating high impact and low impact
		graph drop _all

		* Build a graph like Lemos's, for all pairwise evolutions
		/*
		sum time
		glo tmin=r(min)
		glo tmax=r(max)
		*/
		glo tmin=tq(`tt')+1
		glo tmax=tq(`tt')+13
		glo steps=$tmax-$tmin-1
		glo tl ""
		graph drop _all
		gen lw_real_usd = log(salario_mensual_real/2000)
		forv t=0(1)$steps {
			glo before: di %tqYY!qq $tmin + `t'
			glo after: di %tqYY!qq $tmin + `t' + 1
			sum minw_mensual_real if time==`= $tmin+`t'+1', meanonly
			* glo line=ln(r(mean))
			glo line=ln(r(mean)/2000)
			twoway (kdensity lw_real_usd if time==`=$tmin+`t'' & rif4sample [aw=fex], bw(${bw}) lpattern(solid) lcolor(black)) (kdensity lw_real_usd if time==`=$tmin+`t'+1' & rif4sample [aw=fex], bw(${bw}) lpattern(dash) lcolor(cranberry))  if inrange(lw_real_usd,3.5,5) , xline(${line}, lpattern(dash) lcolor(cranberry)) legend(label(1 "$before") label(2 "$after"))  xtitle(Log wage) ytitle(Density) name(g`t') ylabel(none) graphregion(color(white)) xlabel(3.5(0.25)5, noticks)
			glo tl "${tl} g`t'"
		}
		graph combine $tl, /* title(Density of wages before and after change in MW) subtitle(Formal sector - monthly wages) caption(Line at level of latter minimum wage) */ name(comb) ycommon graphregion(color(white)) cols(3)

		mex formal_den_rob "`master'"
	}
}